package com.rykj.qxj.rest;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.rykj.qxj.annotation.EnableSystemLog;
import com.rykj.qxj.server.common.exception.ApiAbilityStaticsticsIsNullException;
import com.rykj.qxj.server.common.exception.AppIdIsNullException;
import com.rykj.qxj.server.common.exception.ApplyIsNotExistException;
import com.rykj.qxj.server.model.entity.ApiAbilityStatistics;
import com.rykj.qxj.server.model.entity.Notice;
import com.rykj.qxj.server.services.IApiAbilityStatisticsService;
import com.rykj.qxj.util.Result;
import com.techindata.cacheredis.CacheZSet;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;

@Log4j2
@RestController
@RequestMapping("/v1/abilityStatistics")
@Api(tags = "运维监控数据参数管理")
public class ApiAbilityStatisticsController {

	@Autowired
	private IApiAbilityStatisticsService apiAbilityStatisticsService;
	
	private static final String PER_APPLICATION_VISITCOUNT= "perApplicationVisitCount";

	@PostMapping("/saveAbilityStatistics")
	@ApiOperation(value = "保存监控参数信息", notes = "保存监控参数信息")
	@EnableSystemLog(value = "保存监控参数信息", fullPath = "/v1/abilityStatistics/saveAbilityStatistics")
	public Result saveAbilityStatistics(@RequestBody ApiAbilityStatistics apiAbilityStatistics) {
		try {
			apiAbilityStatisticsService.saveApiAbilityStatistics(apiAbilityStatistics);
		} catch (ApplyIsNotExistException e) {
			e.printStackTrace();
			return new Result(false, e.getMessage());
		} catch (AppIdIsNullException e) {
			e.printStackTrace();
			return new Result(false, e.getMessage());
		} catch (ApiAbilityStaticsticsIsNullException e) {
			e.printStackTrace();
			return new Result(false, e.getMessage());
		}
		return new Result(true, "保存成功");
	}

	@GetMapping("/list")
	@ApiOperation(value = "监控参数统计列表", notes = "监控参数统计列表")
	@ApiImplicitParams({
		    @ApiImplicitParam(name = "appKey", value = "应用的appKey", required = true, paramType = "query")})
	public Result selectAbilityStatisticsList(@RequestParam(value = "appKey") String appKey) {
		try {
		Map<String, Object> resultMap = apiAbilityStatisticsService.selectApiAbilityStatisticsList(appKey);
			return new Result(resultMap);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false, "查询失败");
		}
	}
	@GetMapping("/getApplyVisitCount")
	@ApiOperation(value = "获取应用访问数量", notes = "获取应用访问数量")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "appKey", value = "应用的appKey", required = true, paramType = "query")})
	public Result getApplyVisitCount(@RequestParam(value = "appKey") String appKey) {
		try {
			Map<String,Object> result=new HashMap<String, Object>();
			double zscore = CacheZSet.zscore(PER_APPLICATION_VISITCOUNT, appKey);
			//log.info("zscore===="+zscore);  
			if(zscore==-1.0) {
				result.put("applyVisitCount", 0);
			}else {
				result.put("applyVisitCount", zscore);
			}
			return new Result(result); 
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false, "查询失败");
		}
	}

}
